﻿using AutoMapper;
using CarAppDomain.CarParts.Entities;
using CarAppDomain.CarType.Entities;
using CarAppDomain.Rule.Entities;
using CarAppDomain.Sys.Entities;
using CarAppDto.CarParts.CarPartsDtos;
using CarAppDto.CarParts.CarPartsTypeDtos;
using CarAppDto.CarType.AreaDtos;
using CarAppDto.CarType.BrandDtos;
using CarAppDto.CarType.CarDtos;
using CarAppDto.CarType.CarTypeDtos;
using CarAppDto.CarType.CarTypeVersionDtos;
using CarAppDto.CarType.CountryDtos;
using CarAppDto.CarType.VehicleSeriesDtos;
using CarAppDto.Rule.RuleConfigDtos;
using CarAppDto.Sys.AdminDtos;
using D.Util.UtilIoc;
using Newtonsoft.Json;

namespace CarAppAPI.Profiles
{
    [AutoMapperProfile]
    public class ExtPerProfile : Profile
    {
        /// <summary>
        /// 构造函数中创建映射关系
        /// </summary>
        public ExtPerProfile()
        {
            CreateMap<SysAdminEntity, RepAdminInfo>();
            CreateMap<RepAdminInfo, SysAdminEntity>();
            CreateMap<CarTypeEntity, RepQueryCarTypeList>();
            CreateMap<ReqAddCarType, CarTypeEntity>();
            CreateMap<ReqUpdateCarType, CarTypeEntity>();

            CreateMap<RuleConfigEntity, RepRuleConfig>();
            CreateMap<RepRuleConfig, RuleConfigEntity>();
            CreateMap<ReqAddRuleConfig, RuleConfigEntity>();
            CreateMap<ReqUpdateRuleConfig, RuleConfigEntity>();

            CreateMap<ReqAddArea, AreaEntity>();
            CreateMap<AreaEntity, RepArea>();

            CreateMap<ReqAddCountry, CountryEntity>();
            CreateMap<CountryEntity, RepCountry>();

            CreateMap<ReqAddCarPartsType, CarPartsTypeEntity>();
            CreateMap<CarPartsTypeEntity, RepCarPartsType>();

            CreateMap<ReqAddVehicleSeriesCarPartsType, VehicleSeriesCarPartsTypeEntity>();
            CreateMap<VehicleSeriesCarPartsTypeEntity, RepVehicleSeriesCarPartsType>();

            CreateMap<ReqAddCarParts, CarPartsEntity>();
            CreateMap<CarPartsEntity, RepCarParts>();

            CreateMap<ReqAddVehicleSeries, VehicleSeriesEntity>();
            CreateMap<VehicleSeriesEntity, RepVehicleSeries>();

            CreateMap<ReqAddCarType, CarTypeEntity>();
            CreateMap<CarTypeEntity, RepCarType>();

            //品牌
            CreateMap<ReqAddBrand, BrandEntity>();
            CreateMap<BrandEntity, RepBrand>();

            //车型版本
            CreateMap<ReqAddCarTypeVersion, CarTypeVersionEntity>();
            CreateMap<CarTypeVersionEntity, RepCarTypeVersion>();

            //规则
            CreateMap<ReqAddRuleConfig, RuleConfigEntity>();
            CreateMap<RuleConfigEntity, RepRuleConfig>()
                .ForMember(dest => dest.RuleConfigDetailsList, 
                opt => opt.MapFrom(src =>JsonConvert.DeserializeObject<List<RuleConfigDetails>>( src.RuleConfigDetails))); // 指定子对象的映射方式  
           
            //规则匹配项
            CreateMap<ReqAddRuleMapping, RuleMappingEntity>()
                 .ForMember(dest => dest.ValueDetails, opt => opt.MapFrom(src => JsonConvert.SerializeObject(src.Details)));  

            CreateMap<RuleMappingEntity, RepRuleMapping>()
                .ForMember(dest => dest.Details, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<RuleMappingDetails>(src.ValueDetails))); // 指定子对象的映射方式  

            //CreateMap<SysAdminEntity, RepAdminInfo>()
            //    .ForMember(dest => dest.Id, opt => opt.MapFrom(s => s.Id))//指定字段一一对应
            //    .ForMember(dest => dest.UUID, opt => opt.MapFrom(s => s.UUID))
            //    .ForMember(dest => dest.Account, opt => opt.MapFrom(s => s.Account))
            //    .ForMember(dest => dest.NickName, opt => opt.MapFrom(s => s.NickName))
            //    .ForMember(dest => dest.Email, opt => opt.MapFrom(s => s.Email))
            //    .ForMember(dest => dest.UserGroupId, opt => opt.MapFrom(s => s.UserGroupId))
            //    .ForMember(dest => dest.RoleId, opt => opt.MapFrom(s => s.RoleId))
            //    .ForMember(dest => dest.IsPush, opt => opt.MapFrom(s => s.IsPush))
            //    .ForMember(dest => dest.Descption, opt => opt.MapFrom(s => s.Descption));

            // 源实体与目标实体的字段名字是一致的，源实体的字段可以与目标实体中的字段数量不一致。
            // 源实体中的字段名字是Getxxx，而目标实体中对应的字段也可以是xxx。
            // 源实体中包含的实体类字段为Sub,里面包含的字段名字为Age，那么目标实体中对应的字段名字可以是：SubAge。

            // 需要配置才能映射到目标实体
            // var config2 = new MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>()
            // .ForMember(d => d.DestName, opt => opt.MapFrom(s => s.Name))  //指定字段一一对应
            // .ForMember(d => d.Birthday, opt => opt.MapFrom(s => s.Birthday.ToString("yy-MM-dd HH:mm"))) //指定字段按指定的格式转化
            // .ForMember(d => d.Age, opt => opt.Condition(s => s.Age > 5))  //满足条件才赋值
            // .ForMember(d => d.A1, opt => opt.Ignore())  //忽略该字段，不给该字段赋值
            // .ForMember(d => d.A2, opt => opt.NullSubstitute("Default Value"))  //如果源字段值为空，则赋值为 Default Value
            // .ForMember(d => d.A3, opt => opt.MapFrom(s => s.Name + s.Age * 3 + s.Birthday.ToString("d"))) //自己随意组合赋值

            // Mapping
            // 第一次参数是源类型（这里是Model类型），第二个参数是目标类型（这里是DTO类型）
            // CreateMap<Student, StudentDTO>();

            // 使用自定义映射 Student类的ID映射到StudentDTO类的StudentID
            //CreateMap<Student, StudentDTO>()
            //    .ForMember(destinationMember: des => des.StudentID, memberOptions: opt => { opt.MapFrom(mapExpression: map => map.ID); });

            // 对多个属性进行自定义映射
            //CreateMap<Student, StudentDTO>()
            //    .ForMember(destinationMember: des => des.StudentID, memberOptions: opt => { opt.MapFrom(mapExpression: map => map.ID); })
            //    .ForMember(destinationMember: des => des.StudentName, memberOptions: opt => { opt.MapFrom(mapExpression: map => map.Name); })
            //    .ForMember(destinationMember: des => des.StudentAge, memberOptions: opt => { opt.MapFrom(mapExpression: map => map.Age); })
            //    .ForMember(destinationMember: des => des.StudentGender, memberOptions: opt => { opt.MapFrom(mapExpression: map => map.Gender); });
        }
    }
}
